package dyyx;

import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executor;

import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;

public class NacosDemo {
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("hello,nacos");
		
		Properties properties = new Properties();
		// 21.34.53.5:8848,21.34.53.6:8848
        properties.setProperty("serverAddr", "127.0.0.1:8848");
        // properties.setProperty("namespace", "test");
        
        // 服务注册与发现
        NamingService naming = NamingFactory.createNamingService(properties);
        // String serviceName, String ip, int port, String clusterName
        
        String serviceName = "service001";
        //String cluster = "cluster001";
        //String groupName = "group001";
        String ip = "192.168.0.10";
        int port1 = 8888;
        int port2 = 9999;
        
      
        // naming.registerInstance(serviceName, "192.168.0.10", 8888, cluster);
        // naming.registerInstance(serviceName, "192.168.0.10", 9999, cluster);
        
        naming.registerInstance(serviceName,ip, port1);
        naming.registerInstance(serviceName,ip, port2);
        
        List<Instance> instances = naming.getAllInstances(serviceName);
        System.out.println("instances num "+instances.size()+","+instances);
        
        // 取消注册
        naming.deregisterInstance(serviceName,ip, port1);
        
        Thread.sleep(1000);
        
        instances = naming.getAllInstances(serviceName);
        System.out.println("instances num "+instances.size()+","+instances);
          
        // 配置 发布 读取 监听 删除 
        
        String dataId = "test";
        String group = "DEFAULT_GROUP";
        
        ConfigService configService = NacosFactory.createConfigService(properties);
        
        System.out.println("serverStatus="+configService.getServerStatus());
        
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        
        // 
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("receive:" + configInfo);
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });

        // 发布配置 
        boolean isPublishOk = configService.publishConfig(dataId, group, "content");
        System.out.println(isPublishOk);

        Thread.sleep(3000);
        
        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        
        
        isPublishOk = configService.publishConfig(dataId, group, "content2");
        System.out.println(isPublishOk);
        
        Thread.sleep(1000);
        
        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);

        // 删除配置
        boolean isRemoveOk = configService.removeConfig(dataId, group);
        System.out.println(isRemoveOk);
        Thread.sleep(3000);

        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        Thread.sleep(300000);
        
        // curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=test&group=DEFAULT_GROUP&content=HelloWorld"

		
	
	}
}
